/**
 * Created by xiaolinzhang on 2017/7/30.
 */

var async = require('async');
var wxUtil = require('../utils/WxUtil');
var userService = require('../service/UserService');

exports.sharePage = function (req,res,next) {
    var openid = req.query.openid != null ? req.query.openid : req.session.openid;
    async.series({
        shareUrl:function (cb) {
            wxUtil.getShareUrl(openid,cb);
        },
        headimg:function (cb) {
            userService.getWxUser(openid,cb);
        }
    },function (err,result) {
        res.render("share",{title:"分享",shareUrl:result.shareUrl.ticket,headimg:result.headimg[0].head_img,openid:openid})
    });
}

exports.api = function (req,res,next) {
    console.log(req.query.signature);
    console.log(req.query.timestamp);
    console.log(req.query.nonce);
    res.status(200).send(req.query.echostr);
}

exports.getWxConf = function (req,res,next) {
    var openid = req.query.openid != null ? req.query.openid : req.session.openid;
    var url = req.query.url;
    async.series({
        wxconfig:function (cb) {
            wxUtil.getWxConfig(openid,url,function (err,data) {
                var conf = data.wxconf;
                cb(err,{conf:conf})
            })
        }
    },function (err,result) {
        res.status(200).end(JSON.stringify(result.wxconfig));
    })
}